package com.xishu.service;

import com.xishu.bo.SceneCache;
import com.xishu.bo.SceneSwitch;
import com.xishu.config.Constant;
import com.xishu.entity.NameAndId;
import com.xishu.entity.order.Order;
import com.xishu.entity.order.OrderItem;
import com.xishu.entity.shop.*;
import com.xishu.exception.ResponseStatusException;
import com.xishu.response.ResponseStatus;
import com.xishu.response.VerifyUtil;
import com.xishu.service.order.base.OrderService;
import com.xishu.util.DateUtil;
import com.xishu.util.Tools;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static com.xishu.config.Constant.RUN_MODEL_TYPE_FOOD_TABLE_FIX;
import static com.xishu.config.Constant.RUN_MODEL_TYPE_TOTAL;

/**
 * 场景服务
 */
public class SceneService implements Tools {
    private static Logger logger = LoggerFactory.getLogger(SceneService.class);

    private static SceneService instance = new SceneService();
    private CommonService commonService = CommonService.getInstance();

    private SceneService() {
    }

    public static SceneService getInstance() {
        return instance;
    }

    /**
     * 过滤出场景在当前时间范围内
     *
     * @param sceneList
     * @return
     */
    public Scene filterScene(List<Scene> sceneList) {
        sceneList.stream().filter(p -> {
            return isNotEmpty(p.getTimeConfigList());
        }).filter(p -> {
//            p.getTimeConfigList().stream()
            return false;
        });

        return null;
    }

    /**
     * 查询分店下面的所有场景
     *
     * @param shopId
     * @return
     */
    public List<Scene> findSceneListByShopId(Long shopId) {
        Scene scene = new Scene();
        scene.setShopId(shopId);
        scene.setEntity(true);
        return commonService.searchAll(scene);
    }

    /**
     * 查询场景
     *
     * @param sceneId
     * @return
     */
    public Scene findScene(Long sceneId) throws Exception {
        Scene scene = new Scene();
        scene.setId(sceneId);
        return commonService.findEntity(scene, true);
    }

    /**
     * 在分店里面查询场景名称
     *
     * @param sceneName
     * @param shopId
     * @return
     */
    public Optional<Scene> findSceneByName(String sceneName, Long shopId) {
        Scene scene = new Scene();
        scene.setName(sceneName);
        scene.setEntity(true);
        scene.setShopId(shopId);
        return commonService.searchOneOptional(scene);
    }

    /**
     * 判断场景跟当前时间是否匹配
     *
     * @param scene
     * @return
     */
    public boolean isMatchTime(Scene scene) {
        if (isEmpty(scene.getTimeConfigList())) {
            logger.info("scene time is empty");
            return false;
        }

        //有一个时间范围匹配上了就可以了
        return scene.getTimeConfigList().stream().filter(p -> {
            return isMatchTimeConfig(p);
        }).findFirst().isPresent();
    }

    /**
     * 场景的时间配置要匹配当前时间
     *
     * @param sceneTimeConfig
     * @return
     */
    public boolean isMatchTimeConfig(SceneTimeConfig sceneTimeConfig) {
        try {
            if (!isAcrossDay(sceneTimeConfig)) {
                Integer day = sceneTimeConfig.getDay();

                int dayOfWeek = DateUtil.getDayOfWeek(System.currentTimeMillis());
                logger.debug("day of week is {} today is {}", day, dayOfWeek);

                //必须要同一天的
                if (dayOfWeek != day) {
                    logger.info("not the same day");
                    return false;
                }

                //当前时间要在营业时间范围内
                logger.info("start time is {} ,end time is {}", sceneTimeConfig.getStartTime(), sceneTimeConfig.getEndTime());
                return DateUtil.inTimeRange(sceneTimeConfig.getStartTime(), sceneTimeConfig.getEndTime(), System.currentTimeMillis());
            } else {
                //跨天之后特殊处理
                //如果是当天的，那么匹配开始时间到24：00
                //如果是第二天，那么匹配00：00到结束时间
                Integer day = sceneTimeConfig.getDay();

                int dayOfWeek = DateUtil.getDayOfWeek(System.currentTimeMillis());
                logger.info("day of week is {} today is {}", day, dayOfWeek);

                if (dayOfWeek == day) {
                    logger.info("same day, will check start time to 24:00");
                    return DateUtil.inTimeRange(sceneTimeConfig.getStartTime(), "24:00", System.currentTimeMillis());
                } else if (dayOfWeek == DateUtil.getNextWeekDay(day)) {
                    //不是同一天
                    logger.info("next week day, will check 00:00 to end time");
                    return DateUtil.inTimeRange("00:00", sceneTimeConfig.getEndTime(), System.currentTimeMillis());
                } else {
                    //跨天之后，即不是同一天，也不是第二天
                    logger.info("not the same day or the next week day");
                    return false;
                }
            }
        } catch (Exception e) {
            logger.error("e", e);
            return false;
        }
    }

    /**
     * 是否跨天了
     *
     * @param sceneTimeConfig
     * @return
     */
    public boolean isAcrossDay(SceneTimeConfig sceneTimeConfig) {
        String endTime = sceneTimeConfig.getEndTime();
        String startTime = sceneTimeConfig.getStartTime();

        int startInt = DateUtil.hourStringToInt(startTime);
        int endInt = DateUtil.hourStringToInt(endTime);

        return endInt < startInt;
    }

    /**
     * 校验时间配置是否正确
     *
     * @param startTime 开始时间
     * @throws ResponseStatusException
     */
    public void isTimeConfigValid(String startTime, String endTime) throws ResponseStatusException {
        VerifyUtil.verify(() -> startTime != null && startTime.contains(""));
        VerifyUtil.verify(() -> endTime != null && endTime.contains(""));

        VerifyUtil.verify(() -> DateUtil.hourStringToInt(endTime) - DateUtil.hourStringToInt(startTime) > 0, ResponseStatus.END_TIME_MUST_GREATER_THAN_START_TIME);
    }

    /**
     * 校验时间配置是否正确
     *
     * @param timeConfig
     * @throws ResponseStatusException
     */
    public void isTimeConfigValid(SceneTimeConfig timeConfig) throws ResponseStatusException {
        VerifyUtil.verify(() -> timeConfig.getDay() != 0 && timeConfig.getDay() >= 1 && timeConfig.getDay() <= 7);
    }

    /**
     * 时间上是否有重合部分
     *
     * @param timeConfig
     * @param timeConfigList
     */
    public void isTimeConflict(SceneTimeConfig timeConfig, List<SceneTimeConfig> timeConfigList) throws Exception {
        if (isEmpty(timeConfigList)) {
            return;
        }

        Optional<SceneTimeConfig> first = timeConfigList.stream().filter(p -> p.getDay().intValue() == timeConfig.getDay().intValue()).findFirst();
        if (!first.isPresent()) {
            logger.info("not find the same day");
            return;
        }

        //已经是同一天的了，再看时间上有没有重合的
        SceneTimeConfig matchSceneTimeConfig = first.get();
        String startTime = matchSceneTimeConfig.getStartTime();
        String endTime = matchSceneTimeConfig.getEndTime();

        //左闭右开 比如12：00-16：00，就是包含了12:00，到16:00爲止，但不包含16:00
        //已经配置好的开始时间,在这次配置的时间中间
        boolean startTimeMatch = DateUtil.hourStringToInt(startTime) > DateUtil.hourStringToInt(timeConfig.getStartTime())
                && DateUtil.hourStringToInt(startTime) < DateUtil.hourStringToInt(timeConfig.getEndTime());

        //已经配置好的结束时间,在这次配置的时间中间
        boolean endTimeMatch = DateUtil.hourStringToInt(endTime) > DateUtil.hourStringToInt(timeConfig.getStartTime()) &&
                DateUtil.hourStringToInt(endTime) < DateUtil.hourStringToInt(timeConfig.getEndTime());

        //再判断临界条件
        //开始和结束时间不同相等
        VerifyUtil.verify(() -> !startTimeMatch, ResponseStatus.SCENE_TIME_CONFLICT);
        VerifyUtil.verify(() -> !endTimeMatch, ResponseStatus.SCENE_TIME_CONFLICT);
        VerifyUtil.verify(() -> DateUtil.hourStringToInt(startTime) != DateUtil.hourStringToInt(timeConfig.getStartTime()), ResponseStatus.SCENE_TIME_CONFLICT);
        VerifyUtil.verify(() -> DateUtil.hourStringToInt(endTime) != DateUtil.hourStringToInt(timeConfig.getEndTime()), ResponseStatus.SCENE_TIME_CONFLICT);
    }

    /**
     * 判断场景的配置是否已经冲突了
     *
     * @param sceneTimeConfig
     * @param timeConfigList
     */
    public boolean isTimeConfigCheckWithInteger(SceneTimeConfig sceneTimeConfig, List<SceneTimeConfig> timeConfigList) {
        if (isEmpty(timeConfigList)) {
            logger.info("empty time config list");
            return false;
        }

        for (SceneTimeConfig timeConfig : timeConfigList) {
            if (DateUtil.hasSameTime(timeConfig, sceneTimeConfig)) {
                return true;
            }
        }

        return false;
    }


    /**
     * 查询适合的场景
     *
     * @param shopId
     * @throws ResponseStatusException
     */
    public Scene findTheMatchScene(Long shopId) throws Exception {
        Optional<Scene> optionalScene = findSceneOptional(shopId);

        VerifyUtil.verify(() -> optionalScene.isPresent(), ResponseStatus.NOT_MATCH_SCENE);

        return optionalScene.get();
    }

    /**
     * 清除分店的缓存
     *
     * @param shopId
     */
    public void cleanCache(Long shopId) {
        ListCacheService<SceneCache> listCacheService = new ListCacheService<>();
        listCacheService.cleanCache(shopId, SceneCache.class);
    }

    /**
     * 放缓存
     *
     * @param shopId
     */
    public void putCache(Long shopId) throws IOException {
        ListCacheService<SceneCache> listCacheService = new ListCacheService<>();
        Scene scene = new Scene();
        scene.setEntity(true);
        scene.setShopId(shopId);
        List<Scene> sceneList = commonService.searchAll(scene);
        SceneCache sceneCache = new SceneCache(sceneList);
        listCacheService.putCache(shopId, sceneCache);
    }

    /**
     * 从缓存中获取场景
     *
     * @param shopId
     */
    public List<Scene> getCache(Long shopId) throws Exception {
        ListCacheService<SceneCache> listCacheService = new ListCacheService<>();
        SceneCache serviceCache = listCacheService.getCache(shopId, SceneCache.class);

        if (serviceCache == null) {
            return new ArrayList<>();
        }

        List<Scene> sceneList = serviceCache.sceneList;

        if (sceneList == null) {
            return new ArrayList<>();
        }

        return sceneList;
    }

    /**
     * 查询适合的场景
     *
     * @param shopId
     * @return
     */
    public Optional<Scene> findSceneOptional(Long shopId) throws Exception {
        long startTime = System.currentTimeMillis();
        List<Scene> sceneList = null;

        sceneList = getCache(shopId);

        //如果没有找到，刷新缓存
        if (isEmpty(sceneList)) {
            putCache(shopId);
            sceneList = getCache(shopId);
        }

        logger.info("scene list is {}", sceneList.size());
        Optional<Scene> optionalScene = sceneList.stream().filter(p -> getBoolean(p.getEnable())).filter(p -> {
            return SceneService.getInstance().isMatchTime(p);
        }).findFirst();

        logger.info("findSceneOptional cost time {}", System.currentTimeMillis() - startTime);

        return optionalScene;
    }

    /**
     * 通过餐桌来查询场景
     *
     * @param foodTableConfigId
     * @return
     */
    public List<Scene> findSceneByFoodTableConfig(Long foodTableConfigId) {
        Scene scene = new Scene();
        scene.setEntity(true);
        scene.setFoodTableConfigId(foodTableConfigId);
        return commonService.searchAll(scene);
    }

    /**
     * 检查运营模式
     *
     * @param scene
     */
    public void checkRunModule(Scene scene) throws ResponseStatusException {
        if (scene == null) {
            logger.error("scene is null");
            return;
        }

        List<RunModuleScene> runModuleSceneList = scene.getRunModuleSceneList();
        if (isEmpty(runModuleSceneList)) {
            logger.info("runModuleSceneList is empty");
            return;
        }

        runModuleSceneList.forEach(p -> commonService.setDefaultValue(p));

        for (RunModuleScene runModuleScene : runModuleSceneList) {
            //必需要有值
            VerifyUtil.verify(() -> runModuleScene.getType() != null);
        }

        //如果餐桌点餐，餐桌二维码点餐，服务员出单二维码点餐有时，那么总二维点餐，快餐均不能开启，逆向也是同理的
        List<RunModuleScene> enableList = runModuleSceneList.stream().filter(p -> p.getEnable()).collect(Collectors.toList());
        logger.info("enable list is {}", StringUtils.join(enableList, ","));
        List<RunModuleScene> tableRunModule = enableList.stream()
                .filter(p -> p.getType() == Constant.RUN_MODEL_TYPE_FOOD_TABLE || p.getType() == RUN_MODEL_TYPE_FOOD_TABLE_FIX || p.getType() == Constant.RUN_MODEL_TYPE_FOOD_TABLE_WAITER_QR)
                .filter(p -> getBoolean(p.getEnable()))
                .collect(Collectors.toList());

        //客户自己决定的模式
        List<RunModuleScene> customerRunModule = enableList.stream().filter(p -> {
            Integer type = p.getType();
            return type == RUN_MODEL_TYPE_TOTAL || type == Constant.RUN_MODEL_TYPE_QUICK_MODEL;
        }).collect(Collectors.toList());


        List<RunModuleScene> quickTangshiModule = enableList.stream().filter(p -> {
            Integer type = p.getType();
            return type == Constant.RUN_MODEL_TYPE_QUICK_MODEL;
        }).collect(Collectors.toList());

        List<RunModuleScene> totalModule = enableList.stream().filter(p -> {
            Integer type = p.getType();
            return type == RUN_MODEL_TYPE_TOTAL;
        }).collect(Collectors.toList());

        //两种模式不能同时存在
        VerifyUtil.verify(() -> {
            boolean quickTangshi = false;
            if (quickTangshiModule.size() != 0) {
                quickTangshi = true;
            } else if (totalModule.size() != 0) {
                RunModuleScene totalRunModule = totalModule.get(0);
                quickTangshi = getBoolean(totalRunModule.getTangshi());
            }
            return !(tableRunModule.size() != 0 && quickTangshi);
        });

        //两种模式也不能同时为空
        //VerifyUtil.verify(() -> !(tableRunModule.size() == 0 && customerRunModule.size() == 0));
    }

    /**
     * 同步修改场景的名称
     *
     * @param scene
     */
    public void syncToCatalogName(Scene scene) throws Exception {
        if (isEmpty(scene.getName())) {
            return;
        }

        Long sceneId = scene.getId();
        String sceneName = scene.getName();

        if (sceneId == null) {
            return;
        }


        Scene sceneInDb = findScene(sceneId);
        List<Catalog> catalogList = CatalogService.getInstance().findCatalogByShopId(sceneInDb.getShopId());

        //让到有归属这个品类的场景，然后更新
        for (Catalog catalog : catalogList) {
            List<NameAndId> sceneList = catalog.getSceneList();
            if (isEmpty(sceneList)) {
                continue;
            }

            List<NameAndId> nameAndIds = sceneList.stream().filter(p -> equals(p.getId(), sceneId)).collect(Collectors.toList());
            if (isEmpty(nameAndIds)) {
                continue;
            }

            for (NameAndId nameAndId : nameAndIds) {
                nameAndId.setName(sceneName);
            }

            commonService.save(catalog);
        }
    }

    /**
     * 更新菜品里面的场景名称
     *
     * @param scene
     */
    public void syncToItemPriceConfig(Scene scene) throws Exception {
        if (isEmpty(scene.getName())) {
            return;
        }

        Long sceneId = scene.getId();
        String sceneName = scene.getName();

        if (sceneId == null) {
            logger.info("scene id is null");
            return;
        }

        if (isEmpty(sceneName)) {
            logger.info("scene name is null");
            return;
        }

        Scene sceneInDb = findScene(sceneId);
        List<Item> itemList = ItemService.getInstance().findItemByShopId(sceneInDb.getShopId());
        for (Item item : itemList) {
            List<PriceConfig> priceConfigList = item.getPriceConfigList();
            if (isEmpty(priceConfigList)) {
                continue;
            }

            for (PriceConfig priceConfig : priceConfigList) {
                if (equals(priceConfig.getSceneId(), scene.getId())) {
                    priceConfig.setSceneName(sceneName);
                    commonService.save(item);
                    continue;
                }
            }
        }
    }

    /**
     * 同步到推荐菜里面
     *
     * @param scene
     */
    public void syncToSuggestionItem(Scene scene) throws Exception {
        Item item = new Item();
        item.setShopId(scene.getShopId());
        item.setSuggestion(true);
        item.setEntity(true);
        List<Item> itemList = commonService.searchAll(item);

        //推荐菜的处理
        for (Item suggestionItem : itemList) {
            List<NameAndId> sceneList = getList(suggestionItem.getSceneList());
            Optional<NameAndId> nameAndIdOptional = sceneList.stream().filter(s -> equals(s.getId(), scene.getId())).findAny();

            //判断是否该场景有更新
            if (nameAndIdOptional.isPresent()) {
                NameAndId nameAndId = nameAndIdOptional.get();
                nameAndId.setName(scene.getName());
                commonService.save(suggestionItem);
            }
        }
    }

    /**
     * 处理场景切换
     *
     * @param orderInDb
     * @return
     */
    public SceneSwitch sceneSwitch(Order orderInDb) throws Exception {
        SceneSwitch sceneSwitch = new SceneSwitch();
        Long shopId = orderInDb.getShopId();
        Optional<Scene> sceneOptional = SceneService.getInstance().findSceneOptional(shopId);
        Long sceneId = orderInDb.getSceneId();

        if (sceneOptional.isPresent()) {
            logger.info("current scene id {}, order scene id {}", sceneOptional.get().getId(), orderInDb.getSceneId());
        } else {
            logger.info("current scene is not find, order scene id {}", orderInDb.getSceneId());
        }

        //场景没有切换
        if (sceneOptional.isPresent() && equals(sceneOptional.get().getId(), sceneId)) {
            logger.info("scene not switch");
            return sceneSwitch;
        }

        //如果购物车里的菜全部都是当前场景的，那么不用切换
        List<OrderItem> unCheckoutList = getList(orderInDb.getOrderItemDetailList()).stream().filter(p -> !getBoolean(p.getCheckout())).collect(Collectors.toList());
        logger.info("unCheckoutList size is {} sceneOptional present {}", unCheckoutList.size(), sceneOptional.isPresent());
        if (isNotEmpty(unCheckoutList) && sceneOptional.isPresent()) {
            boolean allMatch = unCheckoutList.stream().allMatch(p -> equals(p.getSceneId(), sceneOptional.get().getId()));
            logger.info("allMatch result is {}", allMatch);
            if (allMatch) {
                return sceneSwitch;
            }
        }

        sceneSwitch.setSceneSwitch(true);
        //场景已经不存在了，要么到结账，要么到二维码界面
        if (!sceneOptional.isPresent()) {
            boolean existCheckOut = OrderService.getInstance().existCheckOut(orderInDb);

            if (existCheckOut) {
                logger.info("scene not exist and ");
                sceneSwitch.setToPayPage(true);
                return sceneSwitch;
            }

            logger.info("scene not exist  and empty checkout");
            //没有一个已确认的菜，到二维码界面
            sceneSwitch.setToQrCode(true);
            return sceneSwitch;
        }

        //场景存在，但是场景已经切换了，那么刷新
        logger.info("scene exist and switch");
        sceneSwitch.setToBuy(true);
        return sceneSwitch;
    }

    /**
     * 查询相同的餐桌配置的场景列表
     *
     * @param sceneId
     * @return
     */
    public List<Scene> findSameFoodConfigList(Long sceneId) throws Exception {
        if (sceneId == null) {
            logger.info("sceneId is null");
            return new ArrayList<>();
        }

        Scene scene = findScene(sceneId);
        Long shopId = scene.getShopId();
        Long foodTableConfigId = scene.getFoodTableConfigId();

        if (foodTableConfigId == null) {
            logger.info("foodTableConfigId is null");
            return new ArrayList<>();
        }

        List<Scene> sceneList = ShopService.getInstance().findSceneList(shopId);
        List<Scene> sameFoodTableConfigSceneList = sceneList.stream().filter(s -> !equals(s.getId(), sceneId) && equals(s.getFoodTableConfigId(), foodTableConfigId)).collect(Collectors.toList());

        return sameFoodTableConfigSceneList;
    }

    /**
     * 当前餐桌对应的场景列表
     *
     * @param foodTable
     * @return
     */
    public List<Scene> foodTableMatchSceneList(FoodTable foodTable) throws Exception {
        if (foodTable == null) {
            logger.info("foodTable is null");
            return new ArrayList<>();
        }

        if (foodTable.getSceneId() == null) {
            logger.info("scene id null");
            return new ArrayList<>();
        }

        List<Scene> sameFoodConfigList = findSameFoodConfigList(foodTable.getSceneId());
        Scene scene = commonService.findEntity(foodTable.getSceneId(), Scene.class);
        sameFoodConfigList.add(scene);
        return sameFoodConfigList;
    }

    /**
     * 同步修改B端的值
     * 如果管理系统打开了，默认在B端需要打开
     * 如果管理系统没有操作，那么在B端的配置保留
     *
     * @param scene
     */
    public void syncToModifyInB(Scene scene) {
        List<RunModuleScene> runModuleScenes = getList(scene.getRunModuleSceneList());
        Optional<RunModuleScene> runModuleSceneOptional = runModuleScenes.stream().filter(p -> p.getType() == RUN_MODEL_TYPE_TOTAL).findFirst();

        //如果总二维码的配置存在，并且是开启的
        //那么默认打开B端的配置
        if (runModuleSceneOptional.isPresent() && getBoolean(runModuleSceneOptional.get().getEnable())) {
            logger.info("find the fix food table config {}", scene.getId());
            RunModuleScene runModuleScene = runModuleSceneOptional.get();
            logger.info("take out qr {}, tang shi qr {}", runModuleScene.getTotalTakeOutQr(), runModuleScene.getTotalTangShiQr());
            if (runModuleScene.getTotalTakeOutQr() == null && runModuleScene.getTotalTangShiQr() == null) {
                if (getBoolean(runModuleScene.getTakeout())) {
                    runModuleScene.setTotalTakeOutQr(true);
                }

                if (getBoolean(runModuleScene.getTangshi())) {
                    runModuleScene.setTotalTangShiQr(true);
                }
            }
        }

        //处理餐桌二维码,只要开启了，B端的配置均开启
        runModuleSceneOptional = runModuleScenes.stream().filter(p -> p.getType() == RUN_MODEL_TYPE_FOOD_TABLE_FIX).findFirst();
        if (runModuleSceneOptional.isPresent() && getBoolean(runModuleSceneOptional.get().getEnable())) {
            logger.info("find the fix food table config {}", scene.getId());
            RunModuleScene runModuleScene = runModuleSceneOptional.get();
            logger.info("take out qr {}, tang shi qr {}", runModuleScene.getTotalTakeOutQr(), runModuleScene.getTotalTangShiQr());
            //如果配置均没有传，那么是管理系统的，默认开启
            if (runModuleScene.getTotalTakeOutQr() == null && runModuleScene.getTotalTangShiQr() == null) {
                runModuleScene.setTotalTakeOutQr(true);
                runModuleScene.setTotalTangShiQr(true);
            }
        }

    }

    /**
     * 给定时间是否在时间范围里面
     *
     * @param startTime
     * @param endTime
     * @param time
     * @return
     */
    public boolean inTimeRange(String startTime, String endTime, long time) {
        //如果结束时间比开始时间大，那么直接调用
        int startInt = DateUtil.hourStringToInt(startTime);
        int endInt = DateUtil.hourStringToInt(endTime);
        if (endInt >= startInt) {
            return DateUtil.inTimeRange(startTime, endTime, time);
        }

        //如果结束时间比开始时间小，那么跨天了
        if (DateUtil.inTimeRange(startTime, "24:00", time)) {
            return true;
        }

        return DateUtil.inTimeRange("00:00", endTime, time);
    }

    /**
     * 过滤出当天的配置
     *
     * @param scene
     * @return
     * @throws Exception
     */
    public Optional<SceneTimeConfig> getSceneTimeConfig(Scene scene) throws Exception {
        int dayOfWeek = DateUtil.getDayOfWeek(System.currentTimeMillis());

        //过滤出当天的时间配置
        Optional<SceneTimeConfig> sceneTimeConfigOptional = scene.getTimeConfigList().stream().filter(timeConfig -> {
            return getInt(timeConfig.getDay()) == dayOfWeek;
        }).findAny();

        return sceneTimeConfigOptional;
    }

    /**
     * 将场景的时间转化成普通的时间
     *
     * @param sceneTimeConfig
     * @return
     */
    public TimeConfig sceneTimeToTimeConfig(SceneTimeConfig sceneTimeConfig) {
        TimeConfig timeConfig = new TimeConfig();
        timeConfig.setStartTime(sceneTimeConfig.getStartTime());
        timeConfig.setEndTime(sceneTimeConfig.getEndTime());
        return timeConfig;
    }

}
